<?php
/**
 * CreateUser: zlh
 * CreateTime: 2022/1/27 18:13
 * Remark:
 */

namespace App\Service\Staff;

use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\ResponseFunc;
use App\Func\TokenFuc;
use App\Models\Staff\StaffDepartmentModel;
use App\Models\Staff\StaffModel;
use Exception;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Support\Facades\DB;

class StaffDepartmentService
{
    private StaffDepartmentModel $_staffDepartmentModel;

    public function __construct()
    {
        $this->_staffDepartmentModel = new StaffDepartmentModel();
    }

    /**
     * 修改员工部门
     * @param $staffDepartmentData
     * @return mixed
     */
    public function editStaffDepartment($staffDepartmentData): mixed
    {
        $wheres =[
            "staffId" => $staffDepartmentData['staffId'],
            "staffDepartmentId" => $staffDepartmentData['staffDepartmentId'],
        ];
        unset($staffDepartmentData['staffId']);
        unset($staffDepartmentData['staffDepartmentId']);
        if($staffDepartmentData['is_master'] == 1){
            $this->_staffDepartmentModel->editStaffMasterByStaffId($staffDepartmentData['staff_id']);
        }
        return $this->_staffDepartmentModel->editStaffDepartment($wheres,$staffDepartmentData);
    }

    /**
     * 删除部门用户
     * @param $staffDepartmentData
     * @return mixed
     */
    public function deleteStaffDepartment($staffDepartmentData): mixed
    {
        // 校验当前用户是否只有最后一个部门 如果是 则不允许删除
        $count = $this->_staffDepartmentModel->getStaffDepartmentCount($staffDepartmentData);
        if($count <= 1){
            throw new HttpResponseException(ResponseFunc::returnJson(
                'error',
                null,
                '用户必须属于一个部门',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
                
            ));
        }
        return $this->_staffDepartmentModel->deleteStaffDepartment($staffDepartmentData);
    }

    /**
     * 新增用户部门
     * @param $staffDepartmentData
     * @return mixed
     */
    public function addStaffDepartment($staffDepartmentData) :mixed
    {
        // 这里这样处理是为了对应验证器里面的验证方法
        // 如果出现新的主职位 则覆盖以往主职位
        DB::beginTransaction();
        try {
            if($staffDepartmentData['is_master'] == 1){
                $this->_staffDepartmentModel->editStaffMasterByStaffId($staffDepartmentData['staff_id']);
            }
            $staffDepartmentId =  $this->_staffDepartmentModel->addStaffDepartment($staffDepartmentData);
            if(!$staffDepartmentId){
                throw new HttpResponseException(ResponseFunc::returnJson(
                    'error',
                    null,
                    '新增部门职位失败',
                    ResponseStatusConsts::OK_200_RESPONSE_STATUS,

                ));
            }
            DB::commit();
        }catch (Exception $exception){
            DB::rollBack();
            throw new HttpResponseException(ResponseFunc::returnJson(
                'error',
                null,
                $exception->getMessage(),
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }


        return $this->_staffDepartmentModel->getStaffDepartmentInfoByStaffDepartmentId($staffDepartmentId);


    }
}